using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;

namespace Rosetta.Editor.Collector
{
    [Serializable]
    public class ScriptL10nTrCollector : TextCollectorBase
    {
        protected const string FunStr = "L10n.Tr";
        protected new const string RegexStr = "\\(\\\"[\\w\\W]+?\\\"";

        [LabelText("@Global.GetTr(\"ScriptList\")")]
        public List<MonoScript> ScriptList = new List<MonoScript>();

        private void Refresh()
        {
            DataList = new Dictionary<string, string>();
            for (var i = 0; i < ScriptList.Count; i++)
            {
                var data = ScriptList[i];

                var mats = Regex.Matches(data.text, FunStr + RegexStr);

                for (int j = 0; j < mats.Count; j++)
                {
                    var value = mats[j].Value.Replace(FunStr + "(", "").Trim('"');
                    if (!DataList.ContainsKey(value)) DataList.Add(value, data.name);
                }
            }
        }

        public override void Collect(string space)
        {
            Refresh();
            base.Collect(space);
        }
    }
}